<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>拖放功能</title>
    <style type="text/css">
        #div1 {width:998px; height:66px;padding:10px;border:1px solid #aaaaaa; }
        #drag1{
            margin-right: 6px;
        }
        .go{
            margin-right: 6px;
        }
    </style>
</head>
<body>
    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
        <img id="go1" class="go" src="../images/lefticon1.jpg" draggable="true" ondragstart="drag(event)"/>
        <img id="go2" class="go" src="../images/lefticon2.jpg" draggable="true" ondragstart="drag(event)"/>
        <img id="go3" class="go" src="../images/lefticon3.jpg" draggable="true" ondragstart="drag(event)"/>
        <img id="go4" class="go" src="../images/lefticon4.jpg" draggable="true" ondragstart="drag(event)" />
    </div>
    <img id="drag1" src="../images/lefticon2.jpg" draggable="true" ondragstart="drag(event)"  />

    <script type="text/javascript">
        function allowDrop(ev)
        {
        ev.preventDefault();
        }
        
        function drag(ev)
        {
        ev.dataTransfer.setData("Text",ev.target.id);
        }
        
        function drop(ev)
        {
        console.log(ev.target.nextElementSibling)   //next node
        console.log(ev.target.parentNode)   //farther node

        var parent = ev.target.parentNode;
        var nextChild = ev.target.nextElementSibling;
        ev.preventDefault();
        // debugger
        var data=ev.dataTransfer.getData("Text");
        console.log(data)
        // ev.target.appendChild(document.getElementById(data));
        parent.insertBefore(document.getElementById(data),nextChild)
        }
    </script>
</body>
</html>